package com.linkage.educloud.ah.mvp.presenter.dynamic;

import android.content.Context;
import android.text.TextUtils;

import com.linkage.educloud.ah.Constants;
import com.linkage.educloud.ah.bean.DynamicBean;
import com.linkage.educloud.ah.bean.DynamicCommentBean;
import com.linkage.educloud.ah.bean.DynamicPriasedBean;
import com.linkage.educloud.ah.config.AccountConfig;
import com.linkage.educloud.ah.http.Method;
import com.linkage.educloud.ah.http.RequestUtil;
import com.linkage.educloud.ah.mvp.contract.dynamic.IDynamicDetailContract;
import com.linkage.educloud.ah.mvp.model.dynamic.DynamicDetailModel;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.subsciber.BaseSubscriber;
import com.zzhou.common.library.util.ToastShow;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import io.reactivex.Observable;

/**
 * Created by Mr'Tang on 2018/11/20.
 */

public class DynamicDetailPresenter extends IDynamicDetailContract.IPresenter<IDynamicDetailContract.IView> {
    Context mContext;
    IDynamicDetailContract.IModel iModel;

    public DynamicDetailPresenter(Context context, IDynamicDetailContract.IView iView) {
        super(iView);
        mContext = context;
        iModel = new DynamicDetailModel();
    }

    //班级空间点赞
    @Override
    public void praisedynamicnew(final DynamicBean dynamicBean, String clazz_id, final int type) {
        Observable observable = iModel.request(Method.POST, true, RequestUtil.praisedynamicnew(dynamicBean.getDynamic_id(), clazz_id, type));
        observable.compose(iView.bindLifecycle())
                .subscribe(new BaseSubscriber<String>() {
                    @Override
                    public void onNext(String s) {
                        try {
                            JSONObject json = new JSONObject(s);
                            //{"result":1,"username":"测试七的家长","desc":"点赞成功"}
                            if (json.getInt("result") == Constants.Result.result) {
                                if (type == 0) {//点赞
                                    DynamicPriasedBean bean = new DynamicPriasedBean();
                                    bean.setUser_name(TextUtils.isEmpty(json.getString("username")) ? AccountConfig.getLoginAccount().getUserName() : json.getString("username"));
                                    bean.setUser_id(String.valueOf(AccountConfig.getLoginAccount().getUserId()));
                                    List<DynamicPriasedBean> list = dynamicBean.getPriasedList();
                                    list.add(bean);
                                    dynamicBean.setPriasedList(list);
                                    dynamicBean.setPraisednum(dynamicBean.getPraisednum() + 1);
                                    dynamicBean.setIspriased(0);
                                } else if (type == -1) {//取消赞
                                    for (int i = 0; i < dynamicBean.getPriasedList().size(); i++) {
                                        DynamicPriasedBean result = dynamicBean.getPriasedList().get(i);
                                        if (result.getUser_id().equals(String.valueOf(AccountConfig.getLoginAccount().getUserId()))) {
                                            dynamicBean.getPriasedList().remove(result);
                                        }
                                    }
                                    dynamicBean.setPraisednum(dynamicBean.getPraisednum() - 1);
                                    dynamicBean.setIspriased(-1);
                                }
                                ((IDynamicDetailContract.IView) iView).updatePraise(dynamicBean);
                            } else {
                                ToastShow.show(mContext, json.getString("desc"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                            ToastShow.show(mContext, e.getMessage());
                        }
                    }

                    @Override
                    public void onError(ApiException e) {

                    }
                });
    }

    //删除动态
    @Override
    public void delDynamics(final String id, final int type, final DynamicBean bean) {
        Observable observable = iModel.request(Method.POST, true, RequestUtil.delDynamics(id, type));
        observable.compose(iView.bindLifecycle())
                .subscribe(new BaseSubscriber<String>() {
                    @Override
                    public void onNext(String s) {
                        //body:{"ret":1,"msg":"删除成功"}
                        try {
                            JSONObject json = new JSONObject(s);
                            if (json.getInt("ret") == 1) {
                                if (type == 1) {//删的是动态
                                    ((IDynamicDetailContract.IView) iView).deleteDyanmic();
                                } else if (type == 2) {//删的是评论
                                    for (int i = 0; i < bean.getCommentList().size(); i++) {
                                        DynamicCommentBean result = bean.getCommentList().get(i);
                                        if (result.getC_id().equals(id)) {
                                            bean.getCommentList().remove(result);
                                        }
                                    }
                                    bean.setCommentnum(bean.getCommentnum() - 1);
                                    ((IDynamicDetailContract.IView) iView).changeComment(bean);
                                }
                            } else {
                                ToastShow.show(mContext, json.getString("msg"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                            ToastShow.show(mContext, e.getMessage());
                        }
                    }

                    @Override
                    public void onError(ApiException e) {

                    }
                });
    }

    //评论
    @Override
    public void senddynamicmsg(final DynamicBean bean, String clazz_id, final Long userid, int usertype, final String content) {
        Observable observable = iModel.request(Method.POST, true, RequestUtil.senddynamicmsg(bean.getDynamic_id(), clazz_id, userid, 4, usertype, content, "", -1, -1));
        observable.compose(iView.bindLifecycle())
                .subscribe(new BaseSubscriber<String>() {
                    @Override
                    public void onNext(String s) {
                        //{"c_username":"测试七的家长","comment_id":"1549","result":1,"desc":"操作成功"}
                        try {
                            JSONObject json = new JSONObject(s);
                            if (json.getInt("result") == Constants.Result.result) {
                                DynamicCommentBean commentBean = new DynamicCommentBean();
                                commentBean.setC_id(json.getString("comment_id"));
                                commentBean.setUser_name(json.getString("c_username"));
                                commentBean.setContent(content);
                                commentBean.setUser_id(String.valueOf(userid));
                                bean.getCommentList().add(commentBean);
                                bean.setCommentnum(bean.getCommentnum() + 1);
                                ((IDynamicDetailContract.IView) iView).changeComment(bean);
                            } else {
                                ToastShow.show(mContext, json.getString("desc"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                            ToastShow.show(mContext, e.getMessage());
                        }
                    }

                    @Override
                    public void onError(ApiException e) {

                    }
                });
    }
}
